package com.mowanka.mokeng.app.ext

import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.mowanka.mokeng.R
import com.mowanka.mokeng.app.util.CacheUtil
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @param message 显示对话框的内容 必填项
 * @param title 显示对话框的标题 默认 温馨提示
 * @param positiveButtonText 确定按钮文字 默认确定
 * @param positiveAction 点击确定按钮触发的方法 默认空方法
 * @param negativeButtonText 取消按钮文字 默认空 不为空时显示该按钮
 * @param negativeAction 点击取消按钮触发的方法 默认空方法
 *
 */
fun AppCompatActivity.showMessage(
    message: String,
    title: String = "温馨提示",
    positiveButtonText: String = "确定",
    positiveAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeAction: () -> Unit = {}
) {
    MaterialDialog(this)
        .cancelable(true)
        .lifecycleOwner(this)
        .show {
            title(text = title)
            message(text = message)
            positiveButton(text = positiveButtonText) {
                positiveAction.invoke()
            }
            if (negativeButtonText.isNotEmpty()) {
                negativeButton(text = negativeButtonText) {
                    negativeAction.invoke()
                }
            }
        }
}

/**
 * @param message 显示对话框的内容 必填项
 * @param title 显示对话框的标题 默认 温馨提示
 * @param positiveButtonText 确定按钮文字 默认确定
 * @param positiveAction 点击确定按钮触发的方法 默认空方法
 * @param negativeButtonText 取消按钮文字 默认空 不为空时显示该按钮
 * @param negativeAction 点击取消按钮触发的方法 默认空方法
 */
fun Fragment.showMessage(
    message: String,
    title: String = "温馨提示",
    positiveButtonText: String = "确定",
    positiveAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeAction: () -> Unit = {}
) {
    activity?.let {
        MaterialDialog(it)
            .cancelable(true)
            .lifecycleOwner(viewLifecycleOwner)
            .show {
                title(text = title)
                message(text = message)
                positiveButton(text = positiveButtonText) {
                    positiveAction.invoke()
                }
                if (negativeButtonText.isNotEmpty()) {
                    negativeButton(text = negativeButtonText) {
                        negativeAction.invoke()
                    }
                }
            }
    }
}

/**
 * 拦截登录操作，如果没有登录跳转登录，登录过了贼执行你的方法
 */
fun NavController.jumpByLogin(action: (NavController) -> Unit) {
    if (CacheUtil.isLogin()) {
        action(this)
    } else {
        this.navigateAction(R.id.action_to_loginFragment)
    }
}

/**
 * 拦截登录操作，如果没有登录执行方法 actionLogin 登录过了执行 action
 */
fun NavController.jumpByLogin(
    actionLogin: (NavController) -> Unit,
    action: (NavController) -> Unit
) {
    if (CacheUtil.isLogin()) {
        action(this)
    } else {
        actionLogin(this)
    }
}

inline fun <reified T> Context.pt2px(pt: Int): T = resources.pt2px(pt.toFloat())
inline fun <reified T> Context.pt2px(pt: Float): T = resources.pt2px(pt)

inline fun <reified T> View.pt2px(pt: Int): T = resources.pt2px(pt.toFloat())
inline fun <reified T> View.pt2px(pt: Float): T = resources.pt2px(pt)

inline fun <reified T> Resources.pt2px(pt: Float): T {
    val result = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, pt, displayMetrics)
    return when (T::class) {
        Int::class -> result.toInt() as T
        Float::class -> result as T
        else -> throw IllegalStateException("Type not supported")
    }
}

/**
 * 移除数字小数点后的无用0
 */
fun String.removeZero(): String {
    var result = this
    while (result.contains(".") && result.endsWith("0")) {
        result = result.substringBeforeLast("0")
    }
    if (result.endsWith(".")) {
        result = result.substringBeforeLast(".")
    }
    return result
}

fun Int.timeFormat(): String = if (this >= 10) this.toString() else "0${this}"
fun Long.timeFormat(): String = if (this >= 10) this.toString() else "0${this}"

